using System;
using System.Collections.Generic;
using System.Xml.Linq;
using converter.template;
using converter.template.item;
using SsrsConverter;

namespace converter
{
	// Report Data parsing methods.
	public sealed partial class RdlFile
	{
		// Read in all the data related stuff.
	    private void ParseReportData(XDocument xdoc, XNamespace xmlns, List<string> rdsFiles, Template template)
	    {
	        ParseDataSources(xdoc, xmlns, rdsFiles, template);
	        ParseReportParameters(xdoc, template);
	        ParseDataSets(xdoc, template);
	    }

		// Parses datasources. Datasources could be found in a report definition file or in externally supplied .rds files.
		// Each datasource is uniquely identified by its name.
	    private void ParseDataSources(XDocument xdoc, XNamespace xmlns, List<string> rdsFiles, Template template)
	    {
			// Parse a list of .rds files and add each datasource to the template.
            if (rdsFiles != null)
                foreach (string fileName in rdsFiles)
                {
                    XDocument x = XDocument.Load(fileName);
                    template.AddDataSource(DataSource.Parse(x.Root));
                }

	        // Look up for datasources in the report and if a datasource isn't a reference, add to the template.
	        foreach (XElement ds in xdoc.Root.Descendants(xmlns + "DataSource"))
                template.AddDataSource(DataSource.Parse(ds));
	    }
	}
}